<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Session;
class LoginController extends Controller
{
    //login
    public function login(Request $request)
    {
    	if($request -> isMethod('POST'))
    	{
    		$name = $request -> input('name');
    		$password = $request -> input('password');
    		// dd($name);
    		// $isLogin = \Cookie::get('isLogin');
    		// if($isLogin)
    		// {
    			// $res = DB::table('users') -> where('name', $isLogin) -> first();
    		// 	Session::put('master', $res);
    		// 	return redirect('/home/index') -> with(['info' => '登录成功']);
    		// }

    		$request -> flashOnly('name');
    		// 验证密码
    		$res = DB::table('users') -> where('name', $name) -> first();
    		// dd($res);
    		if(!$res)
    		{
    			return back() -> with(['info' => '用户名未注册']);
    		}
    		// 解密
    		$password = Crypt::decryptString($res -> password);
    		// dd($password);
    		if($request -> input('password') != $password)
    		{
    			return back() -> with(['info' => '用户名或者密码错误']);
    		}

    		Session::put('user_info', $res);
    		return redirect('/home/index') -> with(['info' => '登录成功']);
    	}else
    	{
    		return view('home.user.login', ['title' => '用户登录']);
    	}
    }
    public function logout()
    {
        Session::forget('user_info');
        return redirect('/home/index');
    }
}
